/**
 * 
 */
package com.hlbbcsr.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.hlbbcsr.business.ContractManager;
import com.hlbbcsr.domain.bean.Contract;
import com.hlbbcsr.domain.model.ContractModel;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author CuongTM
 *
 */
/**
 * @author CuongTM
 * 
 */
@Controller("springContractAction")
@Scope("prototype")
public class ContractAction extends ActionSupport implements
		ModelDriven<ContractModel> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ContractModel model = new ContractModel();

	private @Autowired
	ContractManager contractManager;

	public ContractModel getModel() {
		return model;
	}

	static final Logger log = Logger.getLogger(AccountAction.class);

	/**
	 * @return
	 * @throws Exception
	 */
	public String listContract() throws Exception {
		log.info("listContract.start");
		try {
			// get Contract from bussiness Layer
			List<Contract> contractList = contractManager.listContract();

			model.setListContract(contractList);
			System.out
					.println(contractList.get(0).getContractName() + "step 2");
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			throw ex;
		}
		log.info("listContract:end");

		return "success";
	}

	/**
	 * add new contract
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addContract() throws Exception {
		log.info("addContract.start");
		try {
			// Get Contract from screen input
			Contract contract = model.getContract();

			if (contract != null) {
				contractManager.addContract(contract);
			} else {
				return INPUT;
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			throw ex;
		}
		log.info("addContract.end");
		return SUCCESS;
	}

	/**
	 * Search Contract
	 * 
	 * @return
	 * @throws Exception
	 */
	public String searchContract() throws Exception {
		log.info("searchContract.start");
		try {
			Contract contract = model.getContract();

			if (contract != null) {
				// get Contract from bussiness Layer
				log.info("searchContract.start1");
				List<Contract> contractList = contractManager
						.searchEngine(contract);

				model.setListContract(contractList);
				System.out.println(contractList.get(0).getContractName()
						+ "step 2");
			} else {
				return INPUT;
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			throw ex;
		}
		log.info("searchContract.end");
		return SUCCESS;

	}

}
